package com.gdpu.project.action.achievementManager;


import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;


import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.ChengGuoService;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.util.DateUtil;

import com.gdpu.project.vo.CgProject;
import com.gdpu.project.vo.Teacher;
import com.opensymphony.xwork2.ActionSupport;

public class AchievementProjectAction extends ActionSupport {

	private static final long serialVersionUID = 1L;
	private Integer id;
	private String projectId;
	private String fuZeRen;
	private String cooEnterprise;
	private String projApproTime;
	private String projectOrigin;
	private String projectName;
	/**
	 * 以万元为单位
	 */
	private float funds;
	private float keYanFen;
	private float reword;
	private String comment;
	private String teacherId;
	private String teacherName;
	private String projectType;
	private String projectXz;
	private String researchType;
	private Date jieTiDate;
	private int currentpage=1;
	private PageView<CgProject> pageView;
	private HttpServletRequest request;
	private HttpSession session;
	private ChengGuoService chengguoService;
	private TeacherService teacherService;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getProjectId() {
		return projectId;
	}

	public void setProjectId(String projectId) {
		this.projectId = projectId;
	}

	public String getProjectName() {
		return projectName;
	}

	public void setProjectName(String projectName) {
		this.projectName = projectName;
	}

	public String getTeacherId() {
		return teacherId;
	}

	public void setTeacherId(String teacherId) {
		this.teacherId = teacherId;
	}

	public String getTeacherName() {
		return teacherName;
	}

	public void setTeacherName(String teacherName) {
		this.teacherName = teacherName;
	}

	public String getProjectType() {
		return projectType;
	}

	public void setProjectType(String projectType) {
		this.projectType = projectType;
	}

	public String getProjectOrigin() {
		return projectOrigin;
	}

	public void setProjectOrigin(String projectOrigin) {
		this.projectOrigin = projectOrigin;
	}

	public String getProjectXz() {
		return projectXz;
	}

	public void setProjectXz(String projectXz) {
		this.projectXz = projectXz;
	}

	public String getResearchType() {
		return researchType;
	}

	public void setResearchType(String researchType) {
		this.researchType = researchType;
	}

	public String getFuZeRen() {
		return fuZeRen;
	}

	public void setFuZeRen(String fuZeRen) {
		this.fuZeRen = fuZeRen;
	}

	public Date getJieTiDate() {
		return jieTiDate;
	}

	public void setJieTiDate(Date jieTiDate) {
		this.jieTiDate = jieTiDate;
	}

	public float getKeYanFen() {
		return keYanFen;
	}

	public void setKeYanFen(float keYanFen) {
		this.keYanFen = keYanFen;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public void setChengguoService(ChengGuoService chengguoService) {
		this.chengguoService = chengguoService;
	}

	public String getCooEnterprise() {
		return cooEnterprise;
	}

	public String getProjApproTime() {
		return projApproTime;
	}

	public float getFunds() {
		return funds;
	}

	public float getReword() {
		return reword;
	}

	public void setCooEnterprise(String cooEnterprise) {
		this.cooEnterprise = cooEnterprise;
	}

	public void setProjApproTime(String projApproTime) {
		this.projApproTime = projApproTime;
	}

	public void setFunds(float funds) {
		this.funds = funds;
	}

	public void setReword(float reword) {
		this.reword = reword;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	public int getCurrentpage() {
		return currentpage;
	}
	
	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	// 增加科研项目
	public String addAchievementProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgProject project=new CgProject();
		project.setCooEnterprise(cooEnterprise);
		project.setProjApproTime(DateUtil.stringToDate(projApproTime));
		project.setFunds(funds);
		project.setReword(reword);
		project.setComment(comment);
		project.setFuZeRen(fuZeRen);
		project.setProjectName(projectName);
		project.setJieTiDate(getJieTiDate());
		project.setProjectId(getProjectId());
		project.setProjectOrigin(getProjectOrigin());
		project.setProjectType(getProjectType());
		project.setProjectXz(getProjectXz());
		project.setKeYanFen(getKeYanFen());
		project.setResearchType(getResearchType());
		//找到教师（负责人）对应的编号set进去
		List<Teacher> teacherList=teacherService.findByName(fuZeRen);
		if(teacherList.size()>0){
			Teacher teacher=teacherList.get(0);
			project.setTeacherId(teacher.getTeacherId());
		}
		chengguoService.addCgProject(project);
		List<CgProject> cgProjectlist = findAllCgProjectDepRole();
		
		request.setAttribute("cgProjectlist", cgProjectlist);
		return SUCCESS;
	}

	
	
	// 查找所有科研项目
	public String findAllAchievementProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgProject> cgProjectlist = findAllCgProjectDepRole();
		request.setAttribute("cgProjectlist", cgProjectlist);
		return SUCCESS;
	}
	
	// 删除科研项目
	public String delAchievementProjectById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		chengguoService.deleteCgProjectById(getId());
		//角色判断，管理员返回所有，教职工返回相应的
		List<CgProject> cgProjectlist = findAllCgProjectDepRole();
		request.setAttribute("cgProjectlist", cgProjectlist);
		return SUCCESS;
	}

	
	
	// 根据id查找科研项目
	public String findAchievementProjectById() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		CgProject cgProject=chengguoService.findCgProject(getId());
		request.setAttribute("cgProject", cgProject);
		return SUCCESS;
	}
	
	//更新科研项目
	public String updateAchievementProject() throws Exception {
		request = ServletActionContext.getRequest();
		session = request.getSession();			
		CgProject cgProject=chengguoService.findCgProject(getId());
		cgProject.setFuZeRen(getFuZeRen());
		cgProject.setFunds(funds);
		cgProject.setReword(reword);
		cgProject.setCooEnterprise(cooEnterprise);
		cgProject.setProjApproTime(DateUtil.stringToDate(projApproTime));
		cgProject.setProjectName(getProjectName());
		cgProject.setKeYanFen(getKeYanFen());
		cgProject.setComment(getComment());
		cgProject.setJieTiDate(getJieTiDate());
		cgProject.setProjectId(getProjectId());
		cgProject.setProjectOrigin(getProjectOrigin());
		cgProject.setProjectType(getProjectType());
		cgProject.setProjectXz(getProjectXz());
		cgProject.setResearchType(getResearchType());
		
		//set进对应的教师编号
		List<Teacher> teacherList=teacherService.findByName(fuZeRen);
		if(teacherList.size()>0){
			cgProject.setTeacherId(teacherList.get(0).getTeacherId());
		}
		chengguoService.updateCgProject(cgProject);
		List<CgProject> cgProjectlist = findAllCgProjectDepRole();
		request.setAttribute("cgProjectlist", cgProjectlist);
		return SUCCESS;
	}

	
/**
 * 角色判断，管理员返回所有，教职工返回相应的
 * @return
 */
	private List<CgProject> findAllCgProjectDepRole() {
		pageView=new PageView<CgProject>(10,currentpage);//默认是10条记录,第一页;
		Teacher teacher=(Teacher)session.getAttribute("teacher");
		
		QueryResult<CgProject> queryResult=null;
		if(teacher.getRole()==1){
		queryResult=chengguoService.findAllCgProject(pageView.getFirstResult(),pageView.getMaxresult());
		}else{
			queryResult=chengguoService.findAllCgProjectByTeacherId(teacher.getTeacherId(),pageView.getFirstResult(),pageView.getMaxresult());
		}
		pageView.setQueryResult(queryResult);//触发生成页码等等
		request.setAttribute("pageView", pageView);
		return queryResult.getResultlist();
	}
	
}
